using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Csharp01._1118;

namespace Csharp01.TuShuGuanLi
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.bookManagementToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.bookQueryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.bookAddToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.bookEditToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.bookDeleteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.readerManagementToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.readerQueryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.readerAddToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.readerEditToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.borrowManagementToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.borrowBookToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.returnBookToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.systemManagementToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.userManagementToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.dataBackupToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
            this.menuStrip1.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.bookManagementToolStripMenuItem,
            this.readerManagementToolStripMenuItem,
            this.borrowManagementToolStripMenuItem,
            this.systemManagementToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(800, 25);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // bookManagementToolStripMenuItem
            // 
            this.bookManagementToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.bookQueryToolStripMenuItem,
            this.bookAddToolStripMenuItem,
            this.bookEditToolStripMenuItem,
            this.bookDeleteToolStripMenuItem});
            this.bookManagementToolStripMenuItem.Name = "bookManagementToolStripMenuItem";
            this.bookManagementToolStripMenuItem.Size = new System.Drawing.Size(68, 21);
            this.bookManagementToolStripMenuItem.Text = "图书管理";
            // 
            // bookQueryToolStripMenuItem
            // 
            this.bookQueryToolStripMenuItem.Name = "bookQueryToolStripMenuItem";
            this.bookQueryToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.bookQueryToolStripMenuItem.Text = "图书查询";
            this.bookQueryToolStripMenuItem.Click += new System.EventHandler(this.bookQueryToolStripMenuItem_Click);
            // 
            // bookAddToolStripMenuItem
            // 
            this.bookAddToolStripMenuItem.Name = "bookAddToolStripMenuItem";
            this.bookAddToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.bookAddToolStripMenuItem.Text = "添加图书";
            this.bookAddToolStripMenuItem.Click += new System.EventHandler(this.bookAddToolStripMenuItem_Click);
            // 
            // bookEditToolStripMenuItem
            // 
            this.bookEditToolStripMenuItem.Name = "bookEditToolStripMenuItem";
            this.bookEditToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.bookEditToolStripMenuItem.Text = "编辑图书";
            this.bookEditToolStripMenuItem.Click += new System.EventHandler(this.bookEditToolStripMenuItem_Click);
            // 
            // bookDeleteToolStripMenuItem
            // 
            this.bookDeleteToolStripMenuItem.Name = "bookDeleteToolStripMenuItem";
            this.bookDeleteToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.bookDeleteToolStripMenuItem.Text = "删除图书";
            this.bookDeleteToolStripMenuItem.Click += new System.EventHandler(this.bookDeleteToolStripMenuItem_Click);
            // 
            // readerManagementToolStripMenuItem
            // 
            this.readerManagementToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.readerQueryToolStripMenuItem,
            this.readerAddToolStripMenuItem,
            this.readerEditToolStripMenuItem});
            this.readerManagementToolStripMenuItem.Name = "readerManagementToolStripMenuItem";
            this.readerManagementToolStripMenuItem.Size = new System.Drawing.Size(68, 21);
            this.readerManagementToolStripMenuItem.Text = "读者管理";
            // 
            // readerQueryToolStripMenuItem
            // 
            this.readerQueryToolStripMenuItem.Name = "readerQueryToolStripMenuItem";
            this.readerQueryToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.readerQueryToolStripMenuItem.Text = "读者查询";
            this.readerQueryToolStripMenuItem.Click += new System.EventHandler(this.readerQueryToolStripMenuItem_Click);
            // 
            // readerAddToolStripMenuItem
            // 
            this.readerAddToolStripMenuItem.Name = "readerAddToolStripMenuItem";
            this.readerAddToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.readerAddToolStripMenuItem.Text = "添加读者";
            this.readerAddToolStripMenuItem.Click += new System.EventHandler(this.readerAddToolStripMenuItem_Click);
            // 
            // readerEditToolStripMenuItem
            // 
            this.readerEditToolStripMenuItem.Name = "readerEditToolStripMenuItem";
            this.readerEditToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.readerEditToolStripMenuItem.Text = "编辑读者";
            this.readerEditToolStripMenuItem.Click += new System.EventHandler(this.readerEditToolStripMenuItem_Click);
            // 
            // borrowManagementToolStripMenuItem
            // 
            this.borrowManagementToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.borrowBookToolStripMenuItem,
            this.returnBookToolStripMenuItem});
            this.borrowManagementToolStripMenuItem.Name = "borrowManagementToolStripMenuItem";
            this.borrowManagementToolStripMenuItem.Size = new System.Drawing.Size(68, 21);
            this.borrowManagementToolStripMenuItem.Text = "借阅管理";
            // 
            // borrowBookToolStripMenuItem
            // 
            this.borrowBookToolStripMenuItem.Name = "borrowBookToolStripMenuItem";
            this.borrowBookToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.borrowBookToolStripMenuItem.Text = "图书借阅";
            this.borrowBookToolStripMenuItem.Click += new System.EventHandler(this.borrowBookToolStripMenuItem_Click);
            // 
            // returnBookToolStripMenuItem
            // 
            this.returnBookToolStripMenuItem.Name = "returnBookToolStripMenuItem";
            this.returnBookToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.returnBookToolStripMenuItem.Text = "图书归还";
            this.returnBookToolStripMenuItem.Click += new System.EventHandler(this.returnBookToolStripMenuItem_Click);
            // 
            // systemManagementToolStripMenuItem
            // 
            this.systemManagementToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.userManagementToolStripMenuItem,
            this.dataBackupToolStripMenuItem});
            this.systemManagementToolStripMenuItem.Name = "systemManagementToolStripMenuItem";
            this.systemManagementToolStripMenuItem.Size = new System.Drawing.Size(68, 21);
            this.systemManagementToolStripMenuItem.Text = "系统管理";
            // 
            // userManagementToolStripMenuItem
            // 
            this.userManagementToolStripMenuItem.Name = "userManagementToolStripMenuItem";
            this.userManagementToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.userManagementToolStripMenuItem.Text = "用户管理";
            this.userManagementToolStripMenuItem.Click += new System.EventHandler(this.userManagementToolStripMenuItem_Click);
            // 
            // dataBackupToolStripMenuItem
            // 
            this.dataBackupToolStripMenuItem.Name = "dataBackupToolStripMenuItem";
            this.dataBackupToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.dataBackupToolStripMenuItem.Text = "数据备份";
            this.dataBackupToolStripMenuItem.Click += new System.EventHandler(this.dataBackupToolStripMenuItem_Click);
            // 
            // statusStrip1
            // 
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripStatusLabel1});
            this.statusStrip1.Location = new System.Drawing.Point(0, 428);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(800, 22);
            this.statusStrip1.TabIndex = 1;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // toolStripStatusLabel1
            // 
            this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            this.toolStripStatusLabel1.Size = new System.Drawing.Size(136, 17);
            this.toolStripStatusLabel1.Text = "欢迎使用图书管理系统";
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.statusStrip1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "图书管理系统";
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MainForm_FormClosed);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private MenuStrip menuStrip1;
        private ToolStripMenuItem bookManagementToolStripMenuItem;
        private ToolStripMenuItem bookQueryToolStripMenuItem;
        private ToolStripMenuItem bookAddToolStripMenuItem;
        private ToolStripMenuItem bookEditToolStripMenuItem;
        private ToolStripMenuItem bookDeleteToolStripMenuItem;
        private ToolStripMenuItem readerManagementToolStripMenuItem;
        private ToolStripMenuItem readerQueryToolStripMenuItem;
        private ToolStripMenuItem readerAddToolStripMenuItem;
        private ToolStripMenuItem readerEditToolStripMenuItem;
        private ToolStripMenuItem borrowManagementToolStripMenuItem;
        private ToolStripMenuItem borrowBookToolStripMenuItem;
        private ToolStripMenuItem returnBookToolStripMenuItem;
        private ToolStripMenuItem systemManagementToolStripMenuItem;
        private ToolStripMenuItem userManagementToolStripMenuItem;
        private ToolStripMenuItem dataBackupToolStripMenuItem;
        private StatusStrip statusStrip1;
        private ToolStripStatusLabel toolStripStatusLabel1;

        private void bookQueryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 创建图书查询窗体
            BookQueryForm queryForm = new BookQueryForm();
            queryForm.ShowDialog();
        }

        private void bookAddToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BookAddForm addForm = new BookAddForm();
            if (addForm.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("图书添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void bookEditToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("编辑图书功能正在开发中...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void bookDeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BookDeleteForm deleteForm = new BookDeleteForm();
            deleteForm.ShowDialog();
        }

        private void readerQueryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 读者查询功能
            try
            {
                using (var connection = Connection.GetMySqlConnection())
                {
                    connection.Open();
                    string sql = @"
                        SELECT reader_id, reader_card_num, reader_name, reader_gender, reader_phone, 
                               reader_type, register_date,
                               CASE WHEN reader_status = 1 THEN '正常' ELSE '冻结' END as status
                        FROM reader_info
                        LIMIT 20";

                    using (var command = new MySqlCommand(sql, connection))
                    {
                        using (var reader = command.ExecuteReader())
                        {
                            string result = "读者查询结果:\n\n";
                            result += string.Format("{0,-5} {1,-15} {2,-15} {3,-5} {4,-15} {5,-10} {6,-10} {7,-8}\n",
                                "ID", "读者卡号", "姓名", "性别", "电话", "类型", "注册日期", "状态");
                            result += new string('-', 100) + "\n";

                            while (reader.Read())
                            {
                                result += string.Format("{0,-5} {1,-15} {2,-15} {3,-5} {4,-15} {5,-10} {6,-10} {7,-8}\n",
                                    reader.GetInt32("reader_id"),
                                    reader.GetString("reader_card_num"),
                                    reader.GetString("reader_name").Length > 15 ? reader.GetString("reader_name").Substring(0, 12) + "..." : reader.GetString("reader_name"),
                                    reader.GetString("reader_gender"),
                                    reader.GetString("reader_phone"),
                                    reader.GetString("reader_type").Length > 10 ? reader.GetString("reader_type").Substring(0, 7) + "..." : reader.GetString("reader_type"),
                                    reader.GetDateTime("register_date").ToString("yyyy-MM-dd"),
                                    reader.GetString("status"));
                            }

                            MessageBox.Show(result, "读者查询结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询读者失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void readerAddToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("添加读者功能正在开发中...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void readerEditToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("编辑读者功能正在开发中...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void borrowBookToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("图书借阅功能正在开发中...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void returnBookToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("图书归还功能正在开发中...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void userManagementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("用户管理功能正在开发中...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void dataBackupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("数据备份功能正在开发中...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
